﻿using System;

using System.Collections.Generic;
using System.IO;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Controls.Classes;
using Silvermoon.Animations;
using System.Drawing;
using System.Xml;

namespace FileExplorer
{
    public class ViewWeightWindow : Window
    {
        private Toolbar toolbar;
        private BackgroundDecorator background;

        TextBox txtstone;
        TextBox txtpounds;
        TextBox txtDateTime;
        TreeView treeView;

        public ViewWeightWindow()
            : base()
        {
            this.Flags = ControlFlags.IgnoreChildClipping;
            background = new BackgroundDecorator { Color = System.Drawing.Color.Silver };
            background.Margin = new Margin(0);
            Controls.Add(background);


            treeView = new TreeView { Margin = new Margin(0) };
            treeView.ItemHeight = FileItem.ItemHeight;
            treeView.Header.Color = Color.Black;
            treeView.Title = "Weight History";

            FileItemMode.Modes.TreeView = treeView;
           // treeView.PageLoaded += new EventHandler<PageEventArgs>(OnPageLoaded);
            //treeView.PageLoading += new EventHandler<PageEventArgs>(OnPageLoading);
            //treeView.Populate += new EventHandler<TreeViewEventArgs>(OnPopulate);
            //treeView.AttachItem += new EventHandler<TreeViewItemEventArgs>(OnAttachItem);

            treeView.Items.Add(new WaitItem());

            treeView.BeginInit();
            treeView.Items.Clear();
            
            Controls.Add(treeView);
            treeView.Items.Add(new ItemHeader { Text = "Weights", Color = Color.Black });
            TreeViewItem tvi = new TreeViewItem 
            { 
                 Title = "Test"
            };
            //treeView.Items.Add(new FileItem("Test", true));
            treeView.Items.Add(tvi);
            tvi = new TreeViewItem
           {
               Title = "test 2"
               ,
               Button = new Button { Text="test" }
           };

            treeView.Items.Add(tvi);
            treeView.EndInit();
            treeView.Focus();

            toolbar = new Toolbar();
            Toolbar = toolbar;
            toolbar.LeftButton.Text = "Exit";
            toolbar.LeftButton.Click += (s, e) => { Hide(TransitionMode.ZoomOut); };
        }

        private void Populate()
        {
            XmlNodeList oWeights = ListWeightNodes();
            int LabelX = 0;
            foreach (XmlNode oNode in oWeights)
            {
                if (LabelX < Screen.Height - 100)
                {
                    Label lblstone = new Label
                    {
                        Text = oNode.Attributes["DateTime"].Value + " - " + oNode.Attributes["Stones"].Value + " " + oNode.Attributes["Pounds"].Value
                        ,
                        Alpha = 128
                        ,
                        Height = 100
                        ,
                        Width = Screen.Size.Width
                        ,
                        Location = new Point(0, LabelX)
                        ,
                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                    };
                    Controls.Add(lblstone);
                    LabelX = LabelX + 100;
                }
                else
                {
                    break;
                }
            }




        }

        public override void Focus()
        {
            base.Focus();
           // Populate();
        }
        private XmlNodeList ListWeightNodes()
        {
            string sFileName = "";
            StreamReader sr = new StreamReader("debug.txt");
            if (sr.ReadLine() == "true")
            {
                sFileName = "debug.xml";
            }
            else
            {
                sFileName = "MobileLife.xml";
            }
            sr.Close();
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode oRootNode;
            XmlNode oWeightsNode;
            try
            {
                xmlDoc.Load(sFileName);
                oRootNode = xmlDoc.DocumentElement;
            }
            catch (Exception)
            {
                oRootNode = xmlDoc.CreateNode(XmlNodeType.Element, "MobileLife", "");
                xmlDoc.AppendChild(oRootNode);
                oWeightsNode = xmlDoc.CreateNode(XmlNodeType.Element, "Weight", "");
                oRootNode.AppendChild(oWeightsNode);

            }
            XmlNode WeightNode = oRootNode.SelectSingleNode("//Weight");
            
            if (WeightNode == null)
            {
                oWeightsNode = xmlDoc.CreateNode(XmlNodeType.Element, "Weight", "");
                oRootNode.AppendChild(oWeightsNode);
                WeightNode = oRootNode.SelectSingleNode("//Weight");
            }
            return WeightNode.ChildNodes;
        }
    }
}
